let _common = require('../common/common.js');
_common.init(); 

let kpDetal = new Vue({     
	el: '#app',  
	data: { 
		detail: '',
		reply: false
	},        
	computed: {
    	year: function(){
    		let date = this.detail.updated_at;
    		if(!date) return;
    		var obj = utils.formatTime(date);
    		return parseInt(obj.month) + '月' + parseInt(obj.day) + '日';
    	},
    	isLike: function(){
    		return this.detail.isCollect ? 'active' : '';
    	}
	},       
	methods: { 
		getData: function(){
			var that = this; 
			var medicineId;
			var medicineId = medicineId ? medicineId : 2;
			utils.getData('getData','kpDetail',{medicineId: medicineId},function(res){
				if(res.code == 0){
					that.detail = res.data.medicine;
					console.log(that.detail)
				} else {
					console.log(res.message);
				}
			})
		},
		initSwiper: function(){
			let swiper = new Swiper('.swiper-container',{
				direction: 'horizontal',
				observer: true,
				observeParents: true
			})
		},
		//	收藏 
		like: function(event){
			let that = this;
			let target = event.target;
			var data = {
				boxId: this.detail.medicine_id, 
				type: 1
			}
			var url;
			if($(target).hasClass('active')){
				url = 'unLike';
				//	已收藏
				that.likeHandler(url,data,function(){
					$(target).removeClass('active');
				})
			} else {
				url = 'like';
				//	未收藏
				that.likeHandler(url,data,function(){
					$(target).addClass('active');
				})
			}
		},
		likeHandler: function(url,data,cb){
			utils.getData('getData',url,data,function(res){
				if(res.code != 0){
					res.message && alert(res.message);
					return;
				}
				cb(res)
			})
		},
		//	下载
		down: function(){
			let data = {
				url: 'download',
				data: {
					'url': this.detail.voice.url
				}
			}
			utils.getData('getData',data.url,data.data,function(res){
				console.log(res);
			})
		},
		//	触发发表评论
		comment: function(event){
			event.stopPropagation();
			let that = this;
			let target = event.target;
			if(this.reply) return;
			this.reply = true;
			$('.comment').removeClass('hide');
			$('.comment input')[0].focus();
		}, 
		//	发送消息  
		sendMessage: function(event){

			event.stopPropagation();
			let that = this;
			let target = event.target;
			
			//	取参数
			let data = {
				articleId: this.detail.article_id,
				content: $('.comment input').val()
			}
			if(!data.content){
				$('.comment').addClass('hide');
				return;
			}
			utils.getData('getData','comment',data,function(res){
				if(res.code == 0){
					that.reply = false;
					$('.comment').addClass('hide');
					window.location.reload();
				} else {
					res.message && alert(res.message)
				}
			})
		},
		//	隐藏输入框
		hideInput: function(event){
			let that = this;
			$('body').on('click',function(){
				$('.comment').addClass('hide');
				that.reply = false;
			})
		}
	},
	mounted: function(){
		this.getData();
		this.hideInput();
	},
	updated: function(){
		this.initSwiper();
	}
})
